/**
 * Created by Administrator on 2017/3/4.
 */


//导入mongoose模块
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
//设置数据库连接地址(注意的是：指定连接的数据库)
mongoose.connect('mongodb://127.0.0.1:27017/Student');
//连接数据库
var db = mongoose.connection;
//两种状态：连接成功还是连接失败
db.on('error',function(err){
    console.log('数据库连接失败');
    console.log(err.message);
});
db.once('open',function(){
    console.log('数据库连接成功');
});

//获取Schema对象
var Schema = mongoose.Schema;

//创建数据集合（表）
//第一个参数：是表的结构    第二个参数：指定集合（表）名
var userSchema = new Schema({
    id:Number,
    username:String,
    phone:Number,
    psw:String,
    img_url:String,
    permission:Number
},{collection:'User'},{versionKey:false});
var themeSchema = new Schema({
    id:Number,
    content:String,
    content_num:Number,
    like_num:Number,
    com_id:Number,
    user_info:{type:Schema.Types.ObjectId,ref:'User'},
    forum_id:Number,
    time:Date,
    update_time:Date,
    img_url:String,
    out_img:String,
    permission:Number
},{collection:'Theme'},{versionKey:false});
var commentSchema = new Schema({
    id:Number,
    content:String,
    time:Date,
    user_id:Number,
    theme_id:Number,
    img_url:String,
    out_img:String,
    comer_id:String,
    permission:Number
},{collection:'Comment'},{versionKey:false});
var subscribeSchema = new Schema({
    id:Number,
    theme_id:Number,
    user_id:Number,
    update:Boolean,
    theme_info:{type:Schema.Types.ObjectId,ref:'Theme'}
},{collection:'Subscribe'},{versionKey:false});

var mythemeSchema = new Schema({
    user_id:Number,
    theme_id:Number,
    theme_info:{type:Schema.Types.ObjectId,ref:'Theme'}
},{collection:'MyTheme'},{versionKey:false});

var mycommentSchema = new Schema({
    user_id:Number,
    comment_id:Number,
    comment_info:{type:Schema.Types.ObjectId,ref:'Comment'},
    update:Boolean
},{collection:'MyComment'},{versionKey:false});

var historySchema = new Schema({
    user_id:Number,
    theme_id:Number,
    theme_info:{type:Schema.Types.ObjectId,ref:'Theme'}
},{collection:'History'},{versionKey:false});

var forumSchema = new Schema({
    id:Number,
    forum_name:String,
    sub_forum:Object
},{collection:'Forum'},{versionKey:false});

var adminSchema = new Schema({
    id:Number,
    account:Number,
    psw:String
},{collection:'Admin'},{versionKey:false});

var userInfoSchema = new Schema({
    myComCount:Number,
    myTheCount:Number,
    subCount:Number,
    hisCount:Number
},{collection:'UserInfo'},{versionKey:false});

//根据某个集合的几个描述对象，创建这类数据的构造函数
var User = mongoose.model('User',userSchema);
var Theme = mongoose.model('Theme',themeSchema);
var Comment = mongoose.model('Comment',commentSchema);
var Subscribe = mongoose.model('Subscribe',subscribeSchema);
var MyComment = mongoose.model('MyComment',mycommentSchema);
var MyTheme = mongoose.model('MyTheme',mythemeSchema);
var History = mongoose.model('History',historySchema);
var Forum = mongoose.model('Forum',forumSchema);
var UserInfo = mongoose.model('UserInfo',userInfoSchema);


var module_collection = {
    "User":User,
    "Theme":Theme,
    "Comment":Comment,
    "Subscribe":Subscribe,
    "MyComment":MyComment,
    "MyTheme":MyTheme,
    "History":History,
    "Forum":Forum,
    "UserInfo":UserInfo
};

module.exports = module_collection;




